<template>
    <section>
        <DetailsHeader title="发票管理" class="mb-24" />

        <div class="card">
            <div class="card-header" style="margin-bottom:12px;">
                <span class="card-title" style="margin-right: 0;">可开票金额</span>
                <a-tooltip placement="top" title="可开票金额为已充值金额-已开具发票金额，最低开票金额为100元，开票后的金额不可退款">
                    <QuestionCircleOutlined style="margin:0 8px 0 4px;color: #D7D9DD;" />
                </a-tooltip>
            </div>

            <div class="card-content">
                <div class="card-con-lf">
                    <span class="col-money">￥</span>
                    <a-statistic :value="accountInfo?.invoiceInfo?.amount" :precision="2"
                        :value-style="{ color: '#FF3D00' }">
                    </a-statistic>
                </div>
                <div class="card-con-rg">
                    <div>
                        <span class="col-8A93A7 fz-14">企业抬头：</span>
                        <span class="col-223354 fz-14">{{ accountInfo?.invoiceInfo?.title }}</span>
                        <a-button type="link" @click="goInvoiceList">管理</a-button>
                    </div>
                    <div>
                        <span class="col-8A93A7 fz-14">默认地址：</span>
                        <span class="col-223354 fz-14">{{ accountInfo?.invoiceInfo?.address }}</span>
                        <a-button type="link" @click="goAddressList">管理</a-button>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header" style="margin-bottom:24px;">
                <span class="card-title">可开票金额</span>
                <a-select ref="select" v-model:value="queryParams.status" placeholder="发票状态" allowClear @change="getList">
                    <a-select-option value="1">待审核</a-select-option>
                    <a-select-option value="2">开票中</a-select-option>
                    <a-select-option value="3">已开票</a-select-option>
                    <a-select-option value="4">已驳回</a-select-option>
                </a-select>

                <div class="card-opt">
                    <span class="col-8A93A7 fz-12 mr-16" v-if="!userInfo?.userItem?.isEnterprise">本月还可申请{{
                        accountInfo?.invoiceInfo?.count || 0 }}次</span>
                    <a-button type="primary" class="card-opt" @click="handleAdd"
                        :disabled="!(accountInfo?.invoiceInfo?.count > 0 && accountInfo?.invoiceInfo?.amount > 0)">申请开票</a-button>
                </div>
            </div>

            <div class="page-content">
                <a-table :dataSource="tableList" :pagination="false">
                    <a-table-column key="createdAt" title="申请时间" data-index="createdAt" />
                    <a-table-column key="category" title="发票类目" data-index="category" />
                    <a-table-column key="receiptType" title="发票类型" data-index="receiptType">
                        <template #default="{ record }">
                            {{ record.receiptType == '1' ? '专票' : '普票' }}
                        </template>
                    </a-table-column>
                    <a-table-column key="title" title="发票抬头" data-index="title" />
                    <a-table-column key="amount" title="发票金额" data-index="amount" />
                    <a-table-column key="auditStatus" title="发票状态" data-index="auditStatus">
                        <template #default="{ record }">
                            {{ selectDictLabel(auditStatusEnum, record?.auditStatus) }}
                        </template>
                    </a-table-column>
                    <a-table-column key="action" title="操作">
                        <template #default="{ record }">
                            <a-popconfirm title="确定要删除开票信息吗?" @confirm="handelDel(record)">
                                <a-button type="link"
                                    v-if="record.auditStatus == 1 || record.auditStatus == 4" style="padding-left:0;">删除</a-button>
                            </a-popconfirm>
                        </template>
                    </a-table-column>
                </a-table>

                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pn" v-model:limit="queryParams.ps"
                    @pagination="getList" />
            </div>
        </div>

    </section>
</template>
<script setup>
import { ref } from 'vue'
import DetailsHeader from '@/components/common/DetailsHeader.vue'
import { useRouter } from 'vue-router';
import { getApplyInvoiceListApi, delApplyInvoiceApi } from 'api/modules/api.cost'
import userInfoStore from '@/stores/userInfo.js'
import { XsyMessage } from '@/libs/util.toast'
import { selectDictLabel } from '@/libs/util.common'
import { storeToRefs } from 'pinia'
import {
    QuestionCircleOutlined
} from '@ant-design/icons-vue';
const useUserInfoStore = userInfoStore()
const { userInfo, accountInfo } = storeToRefs(useUserInfoStore);

const router = useRouter()

const total = ref(0)
const tableList = ref([])
const queryParams = ref({
    pn: 1,
    ps: 10
})

const auditStatusEnum = [
    {
        label: '待审核',
        value: '1'
    },
    {
        label: '开票中',
        value: '2'
    },
    {
        label: '完成开票',
        value: '3',
    },
    {
        label: '驳回',
        value: '4',
    }
]

const getList = () => {
    getApplyInvoiceListApi(queryParams.value).then(res => {
        total.value = res.data.total
        tableList.value = res.data.list
    })
};

getList()

const handleAdd = () => {
    router.push('/applyInvoice')
}

const goAddressList = () => {
    router.push('/addressList')
}

const goInvoiceList = () => {
    router.push('/invoiceList')
}

// 删除
const handelDel = (row) => {
    delApplyInvoiceApi(row.id).then(res => {
        if (res.code == 0) {
            XsyMessage.success('删除成功');
            getList()
        }
    })
}
</script>

<style scoped lang="scss">
.card {
    background: #fff;
    padding: 20px;
    margin-bottom: 15px;
    box-shadow: 0px 4px 4px 0px #E7EDF8;

    .card-header {
        display: flex;
        align-items: center;
        margin-bottom: 24px;

        .card-title {
            font-weight: 550;
            color: #223354;
            margin-right: 20px;
        }

        .card-opt {
            margin-left: auto;
        }
    }

    .card-content {
        display: flex;
        align-items: center;

        .card-con-lf {
            width: 25%;
            display: flex;
            align-items: center;
        }

        .card-con-rg {
            flex: 1
        }
    }
}
</style>